function addNames() {
    var tr = "<tr><td class='td_left'></td>\n\
<td><input name='name[]' type='text' class='common_txt'  value=''/>\n\
<a onclick=$(this).parent().parent().remove()>删除</a></td></tr>";
    $('#tr_name').before(tr);
}
function changeOrd(obj, table, id) {
    var val = obj.text();
    var c = obj.parent("td");
    obj.parent("td").html("<input type='text' style='width:50px;' onFocus=this.select()  onblur=changeOrdConfirm($(this),'" + table + "'," + id + ") value='" + val + "' />");
    c.children("input").focus();
}
function changeOrdConfirm(obj, table, id) {
    var url = $("#foot").attr("ajax-changeOrdConfirm");
    $.post(url, {
        id: id,
        table: table,
        ord: obj.val()
    }, function(data) {
        obj.parent("td").html("<a onclick=changeOrd($(this),'" + table + "'," + id + ")>" + obj.val() + "</a>");
    })
}
function checkAll(obj) {
    var name = obj.prop("id");
    if (obj.prop("checked") == true) {
        $("input[name='" + name + "']").prop("checked", true);
        if($('#browsebox').length == 1){
        $("input[name='" + name + "']:checked").each(function() {
            if ($('#checkuser').val().indexOf(',' + $(this).val() + ',') == '-1') {
                $('#checkuser').val($('#checkuser').val() + $(this).val() + ',');
            }
        })
    }
    } else {
       $("input[name='" + name + "']").prop("checked", false);
        if($('#browsebox').length == 1){
        $("input[name='" + name + "']").each(function() {
            if ($('#checkuser').val().indexOf(',' + $(this).val() + ',') != '-1') {
                $('#checkuser').val($('#checkuser').val().replace(',' + $(this).val() + ',', ','));
            }
        })
    }
    
    }
}
function checkSingel(obj) {
    var name = obj.prop("name");
    if ($("input[name='" + name + "']:checked").length == $("input[name='" + name + "']").length) {
        $('#' + name).prop("checked", true);
    } else {
        $('#' + name).prop("checked", false)
    }
    if (obj.prop("checked") == true) {
        if ($('#checkuser').val().indexOf(',' + obj.val() + ',') == '-1') {
            $('#checkuser').val(',' + $('#checkuser').val() + obj.val() + ',');
        }
    } else {
        if ($('#checkuser').val().indexOf(',' + obj.val() + ',') != '-1') {
            $('#checkuser').val($('#checkuser').val().replace(',' + obj.val() + ',', ','));
        }
    }
}
function moreDel(table, id) {
    if (id == '' && $("input[name='dels']:checked").length == 0) {
        alert('请选择一条记录');
        return false;
    }
    var word = "确定要批量删除吗";
    if (id > 0) {
        word = "确定要删除该记录吗？"
    }
    if (confirm(word)) {
        var ids = '';
        $("input[name='dels']").each(function() {
            if ($(this).prop("checked") == true) {
                ids += $(this).val() + ",";
            }
        });
        if (id > 0) {
            ids = id;
        }
         var url = $("#foot").attr("ajax-moreDel");
        $.post(url, {
            ids: ids,
            table: table
        }, function(data) {
            location.reload();
        })
    }

}




function singleDel(id, table) {
    var url = $("#foot").attr("url-singleDel");
    if (confirm("确定要删除吗")) {
        $.post(url, {
            ids: id,
            table: table
        }, function(data) {
            $('#del_' + id).remove();

        })
    }
}
function yes(id, table, field) {
    
    var url = $("#foot").attr("url-ajax-yes");
    $.post(url, {
        id: id,
        table: table,
        field: field
    }, function(data) {
        if (data == "是") {
            $('#'+field+'_' + id).html("<a onclick=yes(" + id + ",'" + table + "','" + field + "')>" + data + "</a>");
        } else {
            $('#'+field+'_' + id).html("<a onclick=yes(" + id + ",'" + table + "','" + field + "')><span style='color:red'>" + data + "</span></a>");
        }

    })
}
function addPicList() {
    var tr = "<tr><td class='label'></td>\n\
<td style='text-align:left'> <input type='file' name='pic[]'>\n\
<a onclick='idea_photo_del($(this).parent().parent().remove())'>删除</a>\n\
</td></tr>";
    $('#tr_add').after(tr);
}
function clearTempFile() {
    if (confirm("确定清除缓存么")){
        var url = $("#clearTempFile").attr("data-url");
        $.post(url, {
        }, function(data) {
            alert("清除缓存成功！")
        })
    }
}
function areaChange(obj) {
    var span = obj.parent("span.g-input-selectbox");
    var url = $("#foot").attr("url-getArea");
    if (span.next("span.g-input-selectbox").length == 1) {
        $.post(url, {
            id: obj.val()
        }, function(data) {
            span.nextAll("span.g-input-selectbox").children("select").html("<option value=''>请选择</option>");
            span.next("span.g-input-selectbox").children("select").html("<option value=''>请选择</option>" + data);
        })
    }
}
function choseShop(url) {
    if (url == '') {
         url = $("#foot").attr("url-shops");
    }
    $.post(url, {
    }, function(data) {
        $('#mainbox2').html(data);
        showbgbox3('#mainbox2', 0, 0);
        $(".page a").click(function() {
            var href = $(this).prop("data-href");
            choseShop(href);
        })
    })
}
function in_array(search, array) {
    for (var i in array) {
        if (array[i] == search) {
            return true;
        }
    }
    return false;
}
function clearEmptyArr(arr) {
    var length = arr.length;
    var newArr = [];
    for (var i = 0; i < length; i++) {
        if (arr[i] != '') {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}
function uniQueue(array) {
    var arr = [];
    var m;
    while (array.length > 0) {
        m = array[0];
        arr.push(m);
        array = $.grep(array, function(n, i) {
            return n == m;
        }, true);
    }
    return arr;
} 
